<script setup lang="ts">
  import { ref, defineEmits, computed, watch, onMounted } from 'vue';
  import { useBooleanStore } from '@/store';
  import { getWmsLog } from '@/api/operation-monitoring';

  const emit = defineEmits(['updateArray']);
  const booleanStore = useBooleanStore();
  // 表单查询数据
  const method = ref<string>('');
  const createStartDate = ref<string>('');
  const createEndDate = ref<string>('');
  const rangeDate = ref<Date[]>([]);
  watch(rangeDate, () => {
    if (rangeDate.value && rangeDate.value.length >= 2) {
      createStartDate.value = rangeDate.value[0].toString();
      createEndDate.value = rangeDate.value[1].toString();
    }
  });

  const pageSize = ref<number>(10);
  const pageNumber = ref<number>(1);

  // 表格数据
  async function handleSearch() {
    const newData = await getWmsLog({
      createStartDate: createStartDate.value,
      createEndDate: createEndDate.value,
      method: method.value,
      size: pageSize.value,
      current: pageNumber.value,
    });
    emit('updateArray', newData);
  }
  // 重置
  function reset() {
    createStartDate.value = '';
    createEndDate.value = '';
    method.value = '';
    rangeDate.value = [];
    handleSearch();
  }
  onMounted(() => {
    handleSearch();
  });
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 57) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <div>
        <a-input
          v-model="method"
          placeholder="方法类型"
          size="large"
          allow-clear
        />
      </div>
    </a-col>

    <a-col :span="12">
      <a-range-picker
        v-model:model-value="rangeDate"
        show-time
        format="YYYY-MM-DD HH:mm:ss"
      />
    </a-col>
  </a-row>

  <div class="btn-group">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
  </div>
</template>

<style scoped>
  .btn-group {
    float: right;
    margin-top: 15px;
  }

  .btn-reset {
    margin-left: 10px;
  }
  .row-top {
    margin-top: 10px;
  }
</style>
